package com.netrust.cmsx.core.handler;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.log4j.Logger;

import com.netrust.cmsx.utils.collections.ArrayUtils;

public class MethodExceptionHandler implements MethodInterceptor{
	private final static Logger logger = Logger.getLogger(MethodExceptionHandler.class);
	

	public Object invoke(MethodInvocation invocation) throws Throwable {
		Object result = null;
		try {
			result = invocation.proceed();
		} catch (Throwable e) {
			StringBuffer sb=new StringBuffer("执行"+invocation.getMethod().toString()+"方法时发生异常");
			
			if(ArrayUtils.isNotEmpty(invocation.getArguments())){
				sb.append(",参数为:");
				Object[] objs=invocation.getArguments();
				for (int i = 0; i < objs.length; i++) {
					sb.append("第"+(i+1)+"个参数为:"+objs[i].toString()+"\r");
				};						
			}
			
			System.out.println(sb);
			logger.error(e);
		}

		return result;
	}
    
  
}
